﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.DB;
using System.Security.Cryptography;
using System.IO;

namespace ScheduleMaker
{
    public partial class smAdmUserAddConfig : Form
    {
        DB.Domain.Core.User user;

        public smAdmUserAddConfig( DB.Domain.Core.User _user)
        {
            InitializeComponent();
            this.user = _user;

            lblusername.Text = user.Login;
        }

        private void tbusername_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //user.Password = tbuserpass.Text;
            if (tbuserpass.Text != "")
            {
                Config config = ConfigManager.ConfigManager.getConfiguration();
                Config configForNewUser = new Config();
                configForNewUser.Addr = config.Addr;
                configForNewUser.Login = config.Login;
                configForNewUser.Password = config.Password;
                configForNewUser.Type = config.Type;
                // zmiana hasła w bazie
                user.Password = EncodePassword(tbuserpass.Text);

                try
                {
                    // Sprawdzenie czy dany plik istnieje
                    String path = Application.StartupPath + "\\users\\";
                    if( !Directory.Exists( Application.StartupPath + "\\users\\")) {
                        MessageBox.Show("Katalog users nie istnieje!");
                        DirectoryInfo dr = Directory.CreateDirectory(path);                        
                    }
                    path += user.Login + ".conf";
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    // zapis nowego pliku
                    ConfigManager.ConfigManager.saveConfiguration(path, tbuserpass.Text, config);

                    UserDAO userDao = DAOFactory.getUserDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                    userDao.saveOrUpdate(user);

                    MessageBox.Show("Utworzono plik konfiguracyjny dla użytkownika: "+user.Login+"!");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Błąd podczas tworzenia pliku!");
                }
            }
            else
            {
                MessageBox.Show("Musisz podać hasło!");
            }
            this.Dispose();
        }

        public string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            String md5hash = BitConverter.ToString(encodedBytes);

            return md5hash.Replace("-", "").ToLower();
        }

        private void smAdmUserAddConfig_Load(object sender, EventArgs e)
        {

        }
    }
}
